﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.Xml.Linq;

using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace web.admin
{
    public partial class DiseaseEdit : System.Web.UI.Page
    {
        private DiseaseBLL diseaseBll = DiseaseBLL.Instance;
        private string URL_BACK = "Disease.aspx";
        private int selTypeId = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            try
            {
                int id = RequestTools.GetQueryInt("id", -1);
                if (id > 0)
                {
                    IList list = new ArrayList();
                    DiseaseInfo info = diseaseBll.GetDiseaseInfo(id);
                    if (info != null && info.Id > 0)
                    {
                        this.selTypeId = info.Type;
                        list.Add(info);
                    }

                    this.diseaseInfo.DataSource = list;
                    this.diseaseInfo.DataBind();
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        protected void typeListBound(object sender, EventArgs e)
        {
            try
            {
                //绑定类型列表值
                DropDownList list = (DropDownList)(((DetailsView)sender).FindControl("typeList"));
                list.DataSource = diseaseBll.GetDiseaseTypeList(0); //获得一级分类
                list.DataTextField = "Name";
                list.DataValueField = "Id";
                list.DataBind();

                list.SelectedValue = "" + this.selTypeId;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        protected void UpdateButton_click(object sender, EventArgs e)
        {
            //数据有效性检查
            try
            {
                DiseaseInfo info = validateForm(this.diseaseInfo);
                if (info != null)
                {
                    info.Id = Convert.ToInt32(this.diseaseInfo.DataKey.Value);

                    info.PatientId = UserBLL.Instance.GetUserIdByUserName(info.PatientName);
                    if (info.PatientId > 0)
                    {
                        //准备数据,调用SqlServerDAL执行更新
                        if (diseaseBll.UpdateDisease(info))  //成功
                        {
                            HttpContext.Current.Response.Redirect(URL_BACK, true);
                        }
                    }
                    else
                    {
                        this.errorMsg.Text = "典型病人不存在！";
                        this.errorMsg.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void CancelButton_click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(URL_BACK, true);
        }

        //数据有效性检查
        private DiseaseInfo validateForm(DetailsView formViewCtl)
        {
            DiseaseInfo info = new DiseaseInfo();

            info.Name = ((TextBox)formViewCtl.FindControl("name")).Text;
            info.PinYin = Chinese.GetPingYin(info.Name);
            info.Type = Convert.ToInt32(((DropDownList)formViewCtl.FindControl("typeList")).SelectedValue);
            info.Intro = ((TextBox)formViewCtl.FindControl("introduction")).Text;
            info.Desp = ((TextBox)formViewCtl.FindControl("description")).Text;
            info.PatientName = ((TextBox)formViewCtl.FindControl("ClassPatientName")).Text;
            info.UpdateTime = DateTime.Now;

            return info;
        }
    }
}
